/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        title: '编号',
        type: 'number',
        fastSort: true,
        fastQuery: false
    },
    {
        id: 'accountNo',
        title: '交易账号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'accountPwd',
        title: '交易密码',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'type',
        title: '账号类型',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.investorType.T1,
            2: $lang.investorType.T2
        }
    },
    {
        id: 'status',
        title: '状态',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.investorStatus.T1,
            2: $lang.investorStatus.T2
        }
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        'export':false,
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var option = "";
            var edit = "";
            var investor = record.accountNo;
            if ($("#investorState") && $("#investorState").val() == 1) {
                if (record.status == 1) {
                    option = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='OperateHandle.stateInfo(" + record.id + ",\"" + 2 + "\",\"" + investor + "\")'><i class='fa fa-ban'></i>&nbsp;冻结&nbsp;</a>";
                } else if (record.status == 2) {
                    option = "<a href='javascript:;' class='btn btn-info btn-sm m-r-10' onclick='OperateHandle.stateInfo(" + record.id + ",\"" + 1 + "\",\"" + investor + "\")'><i class='fa fa-plus'></i>&nbsp;启用&nbsp;</a>";
                }
            }
            if ($("#investorEdit") && $("#investorEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }
            return option+edit;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'investor/json/list',
    exportURL: ncGlobal.adminRoot + 'investor/json/export',
    exportFileName: '期货账号列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery|export[excel]',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id"],Timestamp:["createTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

//排序
grid.sortParameter.columnId = ['asc_id',];

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        // 添加投资人对话框
        $('#addModal').on('show.bs.modal', function (event) {
            $addForm = $("#addForm");
            $(".alert-danger").remove();
            $addForm.psly().reset();
            $addForm.find('input[name="accountNo"]').val("");
            $addForm.find('input[name="accountPwd"]').val("");
        });

        //查看对话框初始化
        $("#lookModal").on("show.bs.modal", function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
        });

        // bycj [ 编辑对话框显示时调用 ]
        $('#editModal').on('show.bs.modal', function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),

                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
                //清除错误提示
                editForm.psly().reset();
                modal.find('input[name="id"]').val(gridData.id);
                modal.find('input[name="accountNo"]').val(gridData.accountNo);
                modal.find('input[name="accountPwd"]').val(gridData.accountPwd);
                if(gridData.type == 1){
                   $("#editType1").attr("checked",true);
                }else{
                    $("#editType2").attr("checked",true);
                }
                $(".alert-danger").remove()
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_accountNo'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }


    //状态操作URL
    var stateUrl = ncGlobal.adminRoot + "investor/json/state";
    /**
     * 状态操作
     */
    function _stateInfo(id,status, content) {
        var option='';
        if(status == "1"){
            option += '启用';
        }else{
            option += '冻结';
        }
        var tpl = '您选择对期货账号 <strong>' + content + '</strong>进行'+ option + '操作。<br/>您确定要进行该操作吗?'
        $.ncConfirm({
            url: stateUrl,
            data: {
                id: id,
                status:status
            },
            content: tpl,
            alertTitle: option+"操作"
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
        stateInfo: _stateInfo
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});